package customerEnter.Frame.webService;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import customerEnter.Frame.oss.OssService;
import customerEnter.tool.WhetherToEmpty;

/**
 * osshttp接口實現
 * 
 * 类名称：HttpOssService 类描述： 创建人：dy 创建时间：2016-6-11 下午2:08:20
 * 
 */
@SuppressWarnings("serial")
public class HttpOssService extends HttpServlet {
	@Override
	public void init() throws ServletException {
		OssService.OpenClient();
		super.init();
	}

	@Override
	public void destroy() {
		OssService.CloasClient();
		super.destroy();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(req, resp);
	}

	/**
	 * 上傳doget
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String imgUrl = request.getParameter("imgUrl");
		if (WhetherToEmpty.StringWhetherToEmpty(imgUrl)) {
			out.print("imgUrl:null");
			return;
		}
		try {
			String str = OssService.UploadSystemImg(imgUrl);
			out.print(str);
		} catch (Exception e) {
			out.print("fail:" + e.getMessage());
		}
		out.flush();
		out.close();
	}

	/**
	 * 查詢所有的object
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String pageString = request.getParameter("page");
		String objectName = request.getParameter("objectName");
		int page = 0;
		if (!WhetherToEmpty.StringWhetherToEmpty(pageString)) {
			try {
				page = Integer.parseInt(pageString);
			} catch (Exception e) {
				page = 100;
			}
		}
		java.util.List<String> list = OssService.getAllObject(page, objectName);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("list", list);
		out.print(jsonObject);
		out.flush();
		out.close();
	}
}
